package operation;


import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.tester.ComponentTester;
import abbot.tester.JTreeLocation;
import abbot.tester.JTreeTester;
import robotCross.TextMatcher;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TreeOperation {
	static ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>();
	static JTree tree = null;

	/*查找树节点*/
	public static ArrayList<TreeNode> findTreeNodes(JTree tree,
	                                                ArrayList<SearchMap> mapList) {
		TreeOperation.tree = tree;
		TreeNode root = (TreeNode) tree.getModel().getRoot();
		treeNodes.clear();
		treeNodes.add(root);
		if (mapList == null || mapList.size() == 0) {
			// 选中根节点
			return treeNodes;
		}
		Iterator<SearchMap> iterator = mapList.iterator();
		while (iterator.hasNext()) {
			SearchMap map = iterator.next();
			if (treeNodes.size() == 0)
				return treeNodes;
			visitAllNodes(treeNodes.get(0), map);
		}
		return treeNodes;
	}

	/*遍历子节点*/
	public static void visitAllNodes(TreeNode treeNode, SearchMap map) {
		ArrayList<TreeNode> tmpNodes = new ArrayList<TreeNode>();
		// 按照名称查找所有节点
		if (SearchType.TEXT.equals(map.getType())) {
			// 忽略前后空格
			String nameRegexp = "/[\\s\\t\\n]*" + map.getValue()
					+ "[\\s\\t\\n]*/";
			for (int i = 0; i < treeNode.getChildCount(); i++) {
				JComponent nodeCmp = (JComponent) tree.getCellRenderer()
						.getTreeCellRendererComponent(tree,
								treeNode.getChildAt(i), false, false, false, 0,
								false);
				try {
					JLabel label = (JLabel) new BasicFinder().find(
							(Container) nodeCmp, new TextMatcher(nameRegexp));
					if (label != null) {
						tmpNodes.add(treeNode.getChildAt(i));
					}
				} catch (ComponentNotFoundException
						| MultipleComponentsFoundException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}
		} else if (SearchType.INDEX.equals(map.getType())) {
			Integer[] index = (Integer[]) map.getValue();
			if (index != null && index.length > 0) {
				// 全选
				if (index[0] == -1) {
					for (int i = 0; i < treeNode.getChildCount(); i++) {
						tmpNodes.add(treeNode.getChildAt(i));
						return;
					}
				}
				// 添加指定index节点
				for (int i = 0; i < index.length
						&& i < treeNode.getChildCount(); i++) {
					tmpNodes.add(treeNode.getChildAt(i));
				}

			}
		}
		treeNodes.clear();
		treeNodes.addAll(tmpNodes);
	}

	/*选中所有节点*/
	public static ArrayList<TreeNode> selectTreeNodes(ArrayList<TreeNode> treeNodes) {
		JTreeTester treeTester = new JTreeTester();
//		Info.out("tree node count:" + treeNodes.size());
		Iterator<TreeNode> iterator = treeNodes.iterator();
		//第一个节点不能直接点击ctrl，可能会因为存在选中父节点而不能多选子节点
		if(iterator.hasNext()) {
			DefaultMutableTreeNode firstNode = (DefaultMutableTreeNode) iterator.next();
			TreePath path = new TreePath(firstNode.getPath());
			treeTester.actionSelectPath(tree, path);
			//如果节点已经选中了，需要移动一下鼠标
			int rowForPath = tree.getRowForPath(new TreePath(firstNode.getPath()));
			new ComponentTester().actionMouseMove(tree,new JTreeLocation(rowForPath));
		}
		try {
			// 按住ctrl键为了多选节点
			Operation.robot.keyPress(KeyEvent.VK_CONTROL);
			while (iterator.hasNext()) {
				treeTester.actionSelectPath(tree, new TreePath(
						((DefaultMutableTreeNode) iterator.next()).getPath()));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 释放ctrl键
			Operation.robot.keyRelease(KeyEvent.VK_CONTROL);
		}
		return treeNodes;
	}

	public static ArrayList<TreeNode> selectTreeNodes(JTree tree,
	                                                  ArrayList<SearchMap> mapList) {
		treeNodes = findTreeNodes(tree, mapList);
		selectTreeNodes(treeNodes);
		return treeNodes;
	}

	public static ArrayList<TreeNode> selectTreeNodes(JTree tree, String... path) {
		ArrayList<SearchMap> mapList = new ArrayList();
		for (String pathNode : path) {
			mapList.add(new SearchMap(SearchType.TEXT, pathNode));
		}
		treeNodes = findTreeNodes(tree, mapList);
		selectTreeNodes(treeNodes);
		return treeNodes;
	}

	public static void selectPopupMenuItem(DefaultMutableTreeNode treeNode, String menu) {
		if (menu != null) {
			TreePath treePath = new TreePath(treeNode.getPath());
			new JTreeTester().actionSelectPopupMenuItem(tree,
					new JTreeLocation(treePath), menu);
		}
	}
}
